package gui.windows;

import gui.GUIHandler;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;

import room.Room;
import util.UtilHandler;

import app.AppGlobals;

import config.ConfigHandler;
import config.LauncherHelper;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dialog.ModalExclusionType;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class OptionWindow {

	public JFrame frmHost;
	public Boolean rankedGame = false;
	/**
	 * Create the application.
	 */
	public OptionWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmHost = new JFrame();
		frmHost.setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
		frmHost.setTitle("Options");
		frmHost.setBounds(100, 100, 630, 404);
		frmHost.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmHost.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "User Settings", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 11, 594, 142);
		frmHost.getContentPane().add(panel);
		panel.setLayout(null);
		
		JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Font Settings", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(10, 22, 269, 109);
		panel.add(panel_2);
		
		JLabel lblFontSize = new JLabel("Font Size");
		lblFontSize.setBounds(10, 26, 46, 14);
		panel_2.add(lblFontSize);
		
		JLabel lblGameFont = new JLabel("Game Font");
		lblGameFont.setBounds(10, 51, 66, 14);
		panel_2.add(lblGameFont);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setBounds(105, 48, 154, 20);
		panel_2.add(comboBox);
		
		final JSpinner spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
		spinner.setValue(AppGlobals.launcherConfig.fontSize);
		spinner.setBounds(105, 23, 154, 20);
		panel_2.add(spinner);
		
		JPanel panel_3 = new JPanel();
		panel_3.setLayout(null);
		panel_3.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Launcher Settings", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_3.setBounds(289, 22, 295, 109);
		panel.add(panel_3);
		
		JLabel lblUsername = new JLabel("Default Username");
		lblUsername.setBounds(10, 25, 88, 14);
		panel_3.add(lblUsername);
		
		JLabel lblDefaultDeck = new JLabel("Default Deck");
		lblDefaultDeck.setBounds(10, 50, 88, 14);
		panel_3.add(lblDefaultDeck);
		
		final JComboBox comboBox_1 = new JComboBox(UtilHandler.getDeckList());
		comboBox_1.setBounds(131, 47, 154, 20);
		panel_3.add(comboBox_1);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frmHost.dispose();
			}
		});
		btnCancel.setBounds(515, 332, 89, 23);
		frmHost.getContentPane().add(btnCancel);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(10, 164, 594, 144);
		frmHost.getContentPane().add(panel_1);
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Game Settings", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setLayout(null);
		
		final JCheckBox chckbxCustomSkin = new JCheckBox("Use Custom Skin");
		chckbxCustomSkin.setSelected(AppGlobals.launcherConfig.useSkin);
		chckbxCustomSkin.setBounds(6, 17, 133, 23);
		panel_1.add(chckbxCustomSkin);
		
		final JCheckBox chckbxRandomCardPlacing = new JCheckBox("Random Card Placing");
		chckbxRandomCardPlacing.setSelected(AppGlobals.launcherConfig.randomPlacing);
		chckbxRandomCardPlacing.setBounds(6, 44, 133, 23);
		panel_1.add(chckbxRandomCardPlacing);
		
		final JCheckBox chckbxNoDelayFor = new JCheckBox("No Delay for Chain");
		chckbxNoDelayFor.setSelected(AppGlobals.launcherConfig.noChainDelay);
		chckbxNoDelayFor.setBounds(6, 70, 133, 23);
		panel_1.add(chckbxNoDelayFor);
		
		final JCheckBox chckbxAutoCardPlacing = new JCheckBox("Auto Card Placing");
		chckbxAutoCardPlacing.setSelected(AppGlobals.launcherConfig.autoPlacing);
		chckbxAutoCardPlacing.setBounds(165, 17, 133, 23);
		panel_1.add(chckbxAutoCardPlacing);
		
		final JCheckBox chckbxAutoChainOrder = new JCheckBox("Auto Chain Order");
		chckbxAutoChainOrder.setSelected(AppGlobals.launcherConfig.autoChain);
		chckbxAutoChainOrder.setBounds(165, 44, 133, 23);
		panel_1.add(chckbxAutoChainOrder);
		
		JLabel lblAntialias = new JLabel("Antialias");
		lblAntialias.setBounds(304, 21, 88, 14);
		panel_1.add(lblAntialias);
		
		final JCheckBox chckbxEnableSound = new JCheckBox("Enable Sound");
		chckbxEnableSound.setSelected(AppGlobals.launcherConfig.enableSound);
		chckbxEnableSound.setBounds(300, 44, 133, 23);
		panel_1.add(chckbxEnableSound);
		
		final JCheckBox chckbxEnableDirectx = new JCheckBox("Enable DirectX");
		chckbxEnableDirectx.setSelected(AppGlobals.launcherConfig.enabled3D);
		chckbxEnableDirectx.setBounds(300, 70, 133, 23);
		panel_1.add(chckbxEnableDirectx);
		
		final JCheckBox chckbxEnableMusic = new JCheckBox("Enable Music");
		chckbxEnableMusic.setSelected(AppGlobals.launcherConfig.enableMusic);
		chckbxEnableMusic.setBounds(430, 44, 133, 23);
		panel_1.add(chckbxEnableMusic);
		
		final JCheckBox chckbxFullscreen = new JCheckBox("Fullscreen");
		chckbxFullscreen.setSelected(AppGlobals.launcherConfig.fullscreen);
		chckbxFullscreen.setBounds(430, 70, 133, 23);
		panel_1.add(chckbxFullscreen);
		
		final JSpinner spinner_1 = new JSpinner();
		spinner_1.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
		spinner_1.setValue(AppGlobals.launcherConfig.antialias);
		spinner_1.setBounds(430, 18, 154, 20);
		panel_1.add(spinner_1);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AppGlobals.launcherConfig.fontSize = (int) spinner.getValue();
				AppGlobals.launcherConfig.antialias = (int) spinner_1.getValue();
				AppGlobals.launcherConfig.defaultDeck = comboBox_1.getSelectedItem().toString();
				AppGlobals.launcherConfig.useSkin = chckbxCustomSkin.isSelected();
				AppGlobals.launcherConfig.randomPlacing = chckbxRandomCardPlacing.isSelected();
				AppGlobals.launcherConfig.noChainDelay = chckbxNoDelayFor.isSelected();
				AppGlobals.launcherConfig.autoPlacing = chckbxAutoCardPlacing.isSelected();
				AppGlobals.launcherConfig.autoChain = chckbxAutoChainOrder.isSelected();
				AppGlobals.launcherConfig.enableSound = chckbxEnableSound.isSelected();
				AppGlobals.launcherConfig.enabled3D = chckbxEnableDirectx.isSelected();
				AppGlobals.launcherConfig.enableMusic = chckbxEnableMusic.isSelected();
				AppGlobals.launcherConfig.fullscreen = chckbxFullscreen.isSelected();
				ConfigHandler.saveCurrentConfig();
				frmHost.dispose();
			}
		});
		btnSave.setBounds(416, 332, 89, 23);
		frmHost.getContentPane().add(btnSave);
	}
}
